/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mycompany.psc.entities;

import converters.AlluminiumConverter;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author ahmed
 */
@Entity
@Table(name = "aluminium")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Aluminium.findAll", query = "SELECT a FROM Aluminium a"),
    @NamedQuery(name = "Aluminium.findById", query = "SELECT a FROM Aluminium a WHERE a.id = :id"),
    @NamedQuery(name = "Aluminium.findByTitle", query = "SELECT a FROM Aluminium a WHERE a.title = :title"),
    @NamedQuery(name = "Aluminium.findByTitleAndProject", query = "SELECT a FROM Aluminium a WHERE a.title = :title and a.project=:project"),
    @NamedQuery(name = "Aluminium.findByCodeAndProject", query = "SELECT a FROM Aluminium a WHERE a.code = :code and a.project=:project"),
    @NamedQuery(name = "Aluminium.findByQuantity", query = "SELECT a FROM Aluminium a WHERE a.quantity = :quantity"),
    @NamedQuery(name = "Aluminium.findByDimension", query = "SELECT a FROM Aluminium a WHERE a.dimension = :dimension"),
    @NamedQuery(name = "Aluminium.findByAluRate", query = "SELECT a FROM Aluminium a WHERE a.aluRate = :aluRate"),
    @NamedQuery(name = "Aluminium.findByProject", query = "SELECT a FROM Aluminium a WHERE a.project = :project"),
    @NamedQuery(name = "Aluminium.findByRate", query = "SELECT a FROM Aluminium a WHERE a.rate = :rate")})
public class Aluminium implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "title")
    private String title;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "code")
    private String code;
    @Basic(optional = false)
    @NotNull
    @Column(name = "quantity")
    private int quantity;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 128)
    @Column(name = "dimension")
    private String dimension;
    @Basic(optional = false)
    @NotNull
    @Column(name = "alu_rate")
    private double aluRate;
    @Basic(optional = false)
    @NotNull
    @Column(name = "rate")
    private float rate;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
    private List<AluminiumOffers> aluminiumOffersList;
    @JoinColumn(name = "project_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Projects project;
    @Basic(optional = false)
    @NotNull
    @Column(name = "submit_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date submitDate;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "author")
    private String author;

    public Date getSubmitDate() {
        return submitDate;
    }

    public void setSubmitDate(Date submitDate) {
        this.submitDate = submitDate;
    }

    public String getAuthor() {
        return author;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Aluminium() {
    }

    public Aluminium(Integer id) {
        this.id = id;
    }

    public Aluminium(Integer id, String title, int quantity, String dimension, double aluRate, float rate) {
        this.id = id;
        this.title = title;
        this.quantity = quantity;
        this.dimension = dimension;
        this.aluRate = aluRate;
        this.rate = rate;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getDimension() {
        return dimension;
    }

    public void setDimension(String dimension) {
        this.dimension = dimension;
    }

    public double getAluRate() {
        return aluRate;
    }

    public void setAluRate(double aluRate) {
        this.aluRate = aluRate;
    }

    public float getRate() {
        return rate;
    }

    public void setRate(float rate) {
        this.rate = rate;
    }

    @XmlTransient
    public List<AluminiumOffers> getAluminiumOffersList() {
        return aluminiumOffersList;
    }

    public void setAluminiumOffersList(List<AluminiumOffers> aluminiumOffersList) {
        this.aluminiumOffersList = aluminiumOffersList;
    }

    public Projects getProject() {
        return project;
    }

    public void setProject(Projects projectId) {
        this.project = projectId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Aluminium)) {
            return false;
        }
        Aluminium other = (Aluminium) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.mycompany.psc.entities.Aluminium[ id=" + id + " ]";
    }
    public double getTotalValue(){
        double value=0;
        for(AluminiumOffers a:aluminiumOffersList){
            value+=a.getFinalPrice();
        }
        return value;
    }
    public Aluminium createCopy(Projects target){
        Aluminium copy=new Aluminium();
        copy.setAluRate(aluRate);
        copy.setAuthor(author);
        copy.setCode(code);
        copy.setDimension(dimension);
        copy.setProject(target);
        copy.setQuantity(quantity);
        copy.setRate(rate);
        copy.setSubmitDate(new Date());
        copy.setTitle(title);
        return copy;
    }
    public void copyChildren(Aluminium target){
         for(AluminiumOffers offer:aluminiumOffersList){
            AluminiumOffers newAluminiumOffers=offer.crateCopy();
            newAluminiumOffers.setParent(target);
            target.getAluminiumOffersList().add(newAluminiumOffers);            
        }
    }
}
